package usually;

import java.util.HashMap;
import java.util.Map;

//560. 和为 K 的子数组
public class Num560_subarraySum {
    public int subarraySum(int[] nums, int k) {
        int count = 0,prev = 0;
        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,1);
        for (int num : nums){
            prev += num;
            if (map.containsKey(prev - k)){
                count += map.get(prev - k);
            }
            map.put(prev,map.getOrDefault(prev , 0) + 1);
        }
        return count;
    }
}
